<template>
  <section>

    <el-form :model="loginDto" :rules="rules2" ref="loginFrom" label-position="left" label-width="0px"
             class="demo-ruleForm login-container">
      <h3 class="title">Mall管理系统登录</h3>
      <el-form-item prop="account">
        <el-input type="text" v-model="loginDto.username" auto-complete="off" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input type="password" v-model="loginDto.password" auto-complete="off" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
      <el-form-item style="width:98%;">
        <el-button type="primary" round style="width:102%;" @click.native.prevent="loginIn" :loading="logining">登录
        </el-button>
        <el-button type="info" round style="width: 46%" @click.native.prevent="userRegister">联系方式</el-button>
        <el-button type="danger" round style="width:47%;" @click.native.prevent="shopRegister">商家入驻</el-button>
      </el-form-item>
    </el-form>

    <!--注册弹窗-->
    <el-dialog title="联系方式" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="loginDto" label-width="80px" :rules="rules2" ref="addForm">
        <el-form-item label="用户名">
          <el-input v-model="loginDto.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="text" v-model="loginDto.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input type="text" v-model="loginDto.confirmPassword"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input type="text" v-model="loginDto.email"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input type="text" v-model="loginDto.phone"></el-input>
        </el-form-item>
        <!--        <el-radio-group v-model="loginDto.type">-->
        <!--          <el-radio :label="0">管理员</el-radio>-->
        <!--          <el-radio :label="1">商家</el-radio>-->
        <!--          <el-radio :label="2">用户</el-radio>-->
        <!--        </el-radio-group>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="registerSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>

</template>

<script>
//import NProgress from 'nprogress'
export default {
  data() {
    return {
      logining: false,
      // 显示弹窗
      saveFormVisible: false,
      addLoading: false,

      loginDto: {
        username: '',
        password: '',
        confirmPassword: '',
        email: '',
        phone: ''
      },

      rules2: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        confirmPassword: [
          {required: true, message: '再次输入密码', trigger: 'blur'},
        ]
      },
      checked: true
    };
  },
  methods: {

    // 重置
    handleReset2() {
      this.$refs.loginFrom.resetFields();
    },

    // 登入
    loginIn(ev) {
      var _this = this;
      this.$refs.loginFrom.validate((valid) => {
        if (valid) {
          //_this.$router.replace('/table');
          this.logining = true;
          this.$http.post("/auth/login", this.loginDto)
              .then(result => {
                result = result.data;
                this.logining = false;
                if (result.success) {
                  this.$message({message: "登录成功", type: 'success'});
                  //把后端返回的token和employee存储到localStorage中
                  localStorage.setItem("token", result.resultObj.token);
                  localStorage.setItem("loginUser", JSON.stringify(result.resultObj.loginUser));
                  //跳转首页
                  this.$router.push({path: '/shop'});
                } else {
                  this.$message({message: result.message, type: "error"});
                }
              }).catch(result => {
            this.$message({message: result.message, type: 'error'});
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });

    },

    // 注册弹窗
    userRegister() {
      this.saveFormVisible = true;
    },

    // 注册提交
    registerSubmit() {
      this.$http.post("/auth/register", this.loginDto)
          .then(result => {
            result = result.data;
            this.addLoading  = false;
            if (result.success) {
              this.$message({message: "注册成功", type: 'success'});
              this.saveFormVisible = false;
              //跳转首页
              this.$router.push({path: '/login'});
            } else {
              this.$message({message: result.message, type: "error"});
            }
          }).catch(result => {
        this.$message({message: result.message, type: 'error'});
      })
    },

    shopRegister() {
      this.$router.push({path: '/shopRegister'});
    }
  }
}

</script>

<style lang="scss" scoped>
.login-container {
  /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;

  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }

  .remember {
    margin: 0px 0px 35px 0px;
  }
}
</style>
